/*
 * Android Runner is a multiplayer GPS game fully written by "Xurxo Mendez Perez"
 * 
 * Copyright (C) 2009 "Xurxo Mendez Perez"
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.view.web.components.players.ranking;

import java.util.List;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import es.sonxurxo.androidrunner.model.service.player.to.PublicPlayerProfileTO;
import es.sonxurxo.androidrunner.view.web.application.AndroidRunnerApplication;
import es.sonxurxo.androidrunner.view.web.pages.game.find.list.finished.FinishedGamesListPage;

/**
 * @author "Xurxo Mendez Perez"
 *
 */
public class RankingPanel extends Panel {

	private static final long serialVersionUID = -421820366800799747L;

	public RankingPanel(String id, final int startIndex, final int count) {
		super(id);
		List<PublicPlayerProfileTO> players =
			AndroidRunnerApplication.get().getPlayerService().getOrderedByPoints(startIndex, count);
		WebMarkupContainer playerListBox = new WebMarkupContainer("rankingBox");
		DataView playerList = new DataView("playersList", new ListDataProvider(players), count) {

			private static final long serialVersionUID = -9202521669782650591L;

			@Override
			protected void populateItem(Item item) {
				PublicPlayerProfileTO publicPlayerProfileTO = (PublicPlayerProfileTO)item.getModelObject();

				item.add(new Label("playerRankingPosition", String.valueOf(item.getIndex() + 1)));
				item.add(new Label("playerRankingLogin", publicPlayerProfileTO.getLogin()));
				item.add(new Label("playerRankingPoints", String.valueOf(publicPlayerProfileTO.getTotalPoints())));
				
				if (item.getIndex()%2 == 1) {
                	item.add(new SimpleAttributeModifier("class", "rankingLine-alt"));
                }
			}
		};
		playerListBox.add(playerList);
		playerListBox.add(new BookmarkablePageLink("finishedGamesLink", FinishedGamesListPage.class));
		this.add(playerListBox);
		
		WebMarkupContainer noResultsDiv = new WebMarkupContainer("noResults");
        this.add(noResultsDiv);

        if (players.size() != 0) {
            noResultsDiv.setVisible(false);
        }
	}
}
